<?php


namespace app\mobile\controller;

header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: Authorization, Origin, X-Requested-With, Content-Type, Accept");
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');

use think\facade\Config;
use think\Request;
use think\Db;

class Goods extends \Common1 {

    private $service = "51297647486";
    private $tip = "拍卖须知: 1.本场拍卖公，拍卖向您免收服务费(包括代拍服务费，境外服务费，平台服务费) , 但在您竞得后，结算时，拍行需要向您收取佣金";
    public $token = "";

    public function __construct(Request $request = null){
        parent::__construct();
        if(\request()->action() == "getwatch" || \request()->action() == "getorder" || \request()->action() == "watch" || \request()->action() == "consignbid"){
            $arr = getallheaders();
            $token = $this->Check(isset($arr["Authorization"]) ? $arr["Authorization"] : "");
            $this->token = $token;
        }
    }

    // 获取专场 type=1 未开始和进行中  type=2 结束的 type=3 进行中的专场 注意time格式
    public function GetLots(){
        if(isset($_GET["limit"]) && isset($_GET["page"]) && isset($_GET["type"])){
            $type = $_GET["type"];
            $limit = $_GET["limit"];
            $page = $_GET["page"];
            $subintro = "";
            $sublogo = Config::get("app.ip")."static/image/no_logo.jpg";
            if(!isset($_GET["subid"]) || $_GET["subid"] == 1 || $_GET["subid"] == 0){
                $subname = "拍卖图录";
                if($type == 1){
                    $lots = Db::name("lot")->alias("a")->where("is_del", 0)->where("is_over", 0)->where("state = 0 or state = 1")->order("state desc, start_time asc")->page($page, $limit)->select();
                    $count = Db::name("lot")->where("is_del", 0)->where("is_over", 0)->where("state = 0 or state = 1")->count();
                }else if($type == 2){
                    $lots = Db::name("lot")->where("is_del", 0)->where("is_over", 0)->where("state", 2)->order("correct_end_time desc")->page($page, $limit)->select();
                    $count = Db::name("lot")->where("is_del", 0)->where("is_over", 0)->where("state", 2)->count();
                }else{
                    $lots = Db::name("lot")->where("is_del", 0)->where("is_over", 0)->where("state", 1)->order("correct_start_time asc")->page($page, $limit)->select();
                    $count = Db::name("lot")->where("is_del", 0)->where("is_over", 0)->where("state", 1)->count();
                }
            }else{
                $admin = Db::name("admin")->where("aid", $_GET["subid"])->find();
                $sublogo = Config::get("app.ip")."static/upload/logo/".$admin["logo"];
                $subintro = $admin["intro"];
                $subname = $admin["name"];
                if($type == 1){
                    $lots = Db::name("lot")->where("is_del", 0)->where("aid", $_GET["subid"])->where("is_over", 0)->where("state = 0 or state = 1")->order("state desc, start_time asc")->page($page, $limit)->select();
                    $count = Db::name("lot")->where("is_del", 0)->where("aid", $_GET["subid"])->where("is_over", 0)->where("state = 0 or state = 1")->count();
                }else if($type == 2){
                    $lots = Db::name("lot")->where("is_del", 0)->where("aid", $_GET["subid"])->where("is_over", 0)->where("state", 2)->order("correct_end_time desc")->page($page, $limit)->select();
                    $count = Db::name("lot")->where("is_del", 0)->where("aid", $_GET["subid"])->where("is_over", 0)->where("state", 2)->count();
                }else{
                    $lots = Db::name("lot")->where("is_del", 0)->where("aid", $_GET["subid"])->where("is_over", 0)->where("state", 1)->order("correct_start_time asc")->page($page, $limit)->select();
                    $count = Db::name("lot")->where("is_del", 0)->where("aid", $_GET["subid"])->where("is_over", 0)->where("state", 1)->count();
                }
            }
            $data = [];
            if(!empty($lots)){
                foreach ($lots as $val){
                    $good = Db::name("goods")->where("lid", $val["lid"])->order("number asc")->find();
                    if(!empty($good)){
                        $goodcount = Db::name("goods")->where("lid", $val["lid"])->count();
                        if($val["state"] == 1){
                            $time = date("Y 年 n 月 j 日 H:i", $val["correct_start_time"]);
                        }else if($val["state"] == 0){
                            if(date("Y", $val["start_time"]) == date("Y", $val["end_time"])){
                                if(date("n", $val["start_time"]) == date("n", $val["end_time"])){
                                    if(date("j", $val["start_time"]) == date("j", $val["end_time"])){
                                        $time = date("Y年 n月 j日", $val["start_time"]);
                                    }else{
                                        $time = date("Y年 n月 j日", $val["start_time"]) ."至".date(" j日", $val["end_time"]);
                                    }
                                }else{
                                    $time = date("Y年 n月 j日", $val["start_time"]) ."至".date(" n月 j日", $val["end_time"]);
                                }
                            }else{
                                $time = date("Y年 n月 j日", $val["start_time"]) ."至".date("Y年 n月 j日", $val["end_time"]);
                            }
                        }else{
                            if(date("Y", $val["correct_start_time"]) == date("Y", $val["correct_end_time"])){
                                if(date("n", $val["correct_start_time"]) == date("n", $val["correct_end_time"])){
                                    if(date("j", $val["correct_start_time"]) == date("j", $val["correct_end_time"])){
                                        $time = date("Y年 n月 j日", $val["correct_start_time"]);
                                    }else{
                                        $time = date("Y年 n月 j日", $val["correct_start_time"]) ."至".date(" j日", $val["correct_end_time"]);
                                    }
                                }else{
                                    $time = date("Y年 n月 j日", $val["correct_start_time"]) ."至".date(" n月 j日", $val["correct_end_time"]);
                                }
                            }else{
                                $time = date("Y年 n月 j日", $val["correct_start_time"]) ."至".date("Y年 n月 j日", $val["correct_end_time"]);
                            }
                        }

                        $cover = "";
                        if($val["logo"] != ""){
                            if(is_file("./static/upload/cover/{$val["logo"]}")){
                                $cover = Config::get("app.ip")."static/upload/cover/{$val["logo"]}?t=".time();
                            }else{
                                $image = $this->SearchImg($val["lid"], $good["number"], true);
                                $cover = $image[0];
                            }
                        }


                        $data[] = [
                            "id" => (int)$val["lid"],
                            "cover" => $cover,
                            "name" => $val["name"],
                            "state" => (int)$val["state"],
                            "time" => $time,
                            "total" => $goodcount,
                            "view" => $val["view"],
                        ];
                    }
                }
            }
            $array = [
                "code" => 200,
                "data" => [
                    "list" => $data,
                    "total" => $count,
                    "subname" => $subname,
                    "sublogo" => $sublogo,
                    "subintro" => $subintro
                ]
            ];
        }else{
            $array = [
                "code" => 403,
                "msg" => "服务器错误，请稍后再试"
            ];
        }

        echo json_encode($array, 256);
    }

    //  获取专场详情
    public function GetLotInfo(){
        if(isset($_GET["id"])){
            $lid = $_GET["id"];
            $lot = Db::name("lot")->where("lid", $lid)->find();
            $data = [];
            if(!empty($lot)){

                $view = (int)$lot["view"] + 1;
                $update = [
                    "view" => $view
                ];
                Db::name("lot")->where("lid", $lid)->update($update);

                $good = Db::name("goods")->where("lid", $lid)->order("gid asc")->find();
                $goodcount = Db::name("goods")->where("lid", $lid)->count();

                if($lot["state"] == 1){
                    $time = date("Y 年 n 月 j 日 H:i", $lot["correct_start_time"]);
                }else if($lot["state"] == 0){
                    if(date("Y", $lot["start_time"]) == date("Y", $lot["end_time"])){
                        if(date("n", $lot["start_time"]) == date("n", $lot["end_time"])){
                            if(date("j", $lot["start_time"]) == date("j", $lot["end_time"])){
                                $time = date("Y年 n月 j日", $lot["start_time"]);
                            }else{
                                $time = date("Y年 n月 j日", $lot["start_time"]) ."至".date(" j日", $lot["end_time"]);
                            }
                        }else{
                            $time = date("Y年 n月 j日", $lot["start_time"]) ."至".date(" n月 j日", $lot["end_time"]);
                        }
                    }else{
                        $time = date("Y年 n月 j日", $lot["start_time"]) ."至".date("Y年 n月 j日", $lot["end_time"]);
                    }
                }else{
                    if(date("Y", $lot["correct_start_time"]) == date("Y", $lot["correct_end_time"])){
                        if(date("n", $lot["correct_start_time"]) == date("n", $lot["correct_end_time"])){
                            if(date("j", $lot["correct_start_time"]) == date("j", $lot["correct_end_time"])){
                                $time = date("Y年 n月 j日", $lot["correct_start_time"]);
                            }else{
                                $time = date("Y年 n月 j日", $lot["correct_start_time"]) ."至".date(" j日", $lot["correct_end_time"]);
                            }
                        }else{
                            $time = date("Y年 n月 j日", $lot["correct_start_time"]) ."至".date(" n月 j日", $lot["correct_end_time"]);
                        }
                    }else{
                        $time = date("Y年 n月 j日", $lot["correct_start_time"]) ."至".date("Y年 n月 j日", $lot["correct_end_time"]);
                    }
                }



                $lotnumber = Db::name("goods")->where("lid", $lot["lid"])->where("state", 1)->count();

                $admin = Db::name("admin")->where("aid", $lot["aid"])->find();

                if($admin["logo"] != ""){
                    $logo = Config::get("app.ip")."static/upload/logo/{$admin["logo"]}?t=".time();
                    $cover = Config::get("app.ip")."static/upload/logo/{$admin["logo"]}?t=".time();
                }else{
                    $image = $this->SearchImg($lot["lid"], $good["number"], true);
                    $logo = $image[0];
                    $cover = $image[0];
                }
                $admin = Db::name("admin")->where("aid", $lot["aid"])->find();
                $data = [
                    "id" => (int)$lot["lid"],
                    "subid" => $lot["aid"],
                    "cover" => $cover,
                    "name" => $lot["name"],
                    "state" => (int)$lot["state"],
                    "time" => $time,
                    "total" => $goodcount,
                    "view" => $view,
                    "logo" => $logo,
                    "intro" => $admin["intro"] == "" ? "本场拍品{$lotnumber}件"."\n"."拍卖日期: ".$time : $admin["intro"]." - 本场拍品{$lotnumber}件"."\n"."拍卖日期: ".$time
                ];
            }
            $array = [
                "lot" => $data,
                "service" => $this->service,
                "tip" => $this->tip
            ];
            Code(200, false, $array);
        }else{
            Code(403, "服务器错误，请稍后再试");
        }
    }

    // 获取专场商品
    public function GetLotGoods(){
        if(isset($_GET["id"]) && isset($_GET["page"]) && isset($_GET["limit"])){
            $id = $_GET["id"];
            $page = $_GET["page"];
            $limit = $_GET["limit"];
            if(!isset($_GET["key"]) || (isset($_GET["key"]) && $_GET["key"] == "")){
                $goods = Db::name("goods")->where("lid", $id)->order("number asc")->page($page, $limit)->select();
            }else{
                $goods = Db::name("goods")->where("lid", $id)->where("number", $_GET["key"])->order("number asc")->page($page, $limit)->select();
            }
            $data = [];
            if(!empty($goods)){
                foreach ($goods as $val){
                    $image = $this->SearchImg($val["lid"], $val["number"]);
                    $data[] = [
                        "id" => (int)$val["gid"],
                        "cover" => $image[0],
                        "name" => $val["name"],
                        "final" => $val["final_price"] == 0 ? "" : $val["final_price"],
                        "lot" => $val["number"],
                        "start" => number_format($val["price"], 0),
                        "start_unit" => "￥",
                        "state" => (int)$val["state"],
                    ];
                }
            }
            Code(200, false, $data);
        }else{
            Code(403, "服务器错误，请稍后再试");
        }
    }

    // 获取商品详情
    public function GetGoodsDetail(){
        if(isset($_GET["id"])){
            $arr = getallheaders();
            if(isset($arr["Authorization"])){
                $this->token = $this->Check(isset($arr["Authorization"]) ? $arr["Authorization"] : "");
                $watch = Db::name("watched")->where("gid", $_GET["id"])->where("ub_id", $this->token["id"])->find();
                $watched = !empty($watch);
            }else{
                $watched = false;
            }
            $id = $_GET["id"];
            $good = Db::name("goods")->where("gid", $id)->find();
            $lot = Db::name("lot")->where("lid", $good["lid"])->find();
            $start_time = $this->TransformTime($lot["start_time"], '0', '0', '0');

            /* 制作spec  json字符串 */
            $spec = spec($good, 2);

            /* 转换价格 */
            $eval = eval_price($good, 2);

            $image = $this->SearchImg($lot["lid"], $good["number"]);
            $data = [
                "id" => $good["gid"],
                "belong" => [
                    "id" => (int)$lot["lid"],
                    "name" => $lot["name"],
                    "subid" => $lot["aid"]
                ],
                "final" => $good["state"] == 3 ? $good["final_price"] == 0 ? "流拍" : $good["final_price"] : "",
                "img" => $image,
                "lot" => $good["number"],
                "name" => $good["name"],
                "service" => $this->service,
                "spec" => $spec,
                "start" => number_format($good["price"], 0),
                "start_convert" => "￥ ".number_format((int)$good["price"] * (float)$lot["rate"], 0),
                "start_unit" => "￥",
                "state" => time() >= $start_time ? ( $lot["state"] == 2 ? 2 : ( $good["state"] > 3 ? 2 : 1 ) ) : 0,
                "watched" => $watched,
                "eval" => $eval
            ];
            Code(200, false, $data);
        }else{
            Code(403, "服务器错误，请稍后再试");
        }
    }

    // 关注
    public function Watch(){
        if(isset($_POST["id"]) && isset($_POST["watched"])){
            $uid = $this->token["id"];
            $gid = $_POST["id"];
            $watched = $_POST["watched"];
            if($watched == "true"){
                $wat = Db::name("watched")->where("ub_id", $uid)->where("gid", $gid)->find();
                if(empty($wat)){
                    $insert = [
                        "ub_id" => $uid,
                        "gid" => $gid,
                    ];
                    Db::name("watched")->insert($insert);
                }
                $msg = "关注成功";
            }else{
                Db::name("watched")->where("ub_id", $uid)->where("gid", $gid)->delete();
                $msg = "取消成功";
            }
            $data = [
                "watched" => $watched == "true"
            ];
            Code(200, $msg, $data);
        }else{
            Code(403, "服务器错误，请稍后再试");
        }
    }

    // 委托价格
    public function ConsignBid(){
        if(isset($_POST["id"]) && isset($_POST["bid"])){
            $uid = $this->token["id"];
            $gid = $_POST["id"];
            $bid = $_POST["bid"];
            $wat = Db::name("consigns")->where("ub_id", $uid)->where("gid", $gid)->find();
            $num = Db::name("num")->where("ub_id", $uid)->find();
            $good = Db::name("goods")->where("gid", $gid)->find();
            $lot = Db::name("lot")->where("lid", $good["lid"])->find();
            $pay_com = Db::name("user_buy_company")->where("ub_id", $uid)->where("aid", $lot["aid"])->find();
            if(empty($num)){
                Code(403, "请先绑定号码牌");
            }else{
                if($pay_com["pay_state"] == 0){
                    Code(403, "您不是该代理下的vip，请前去交保证金");
                }else{
                    if(empty($wat)){
                        $num = Db::name("num")->where("ub_id", $uid)->find();
                        $insert = [
                            "ub_id" => $uid,
                            "gid" => $gid,
                            "lid" => $good["lid"],
                            "price" => $bid
                        ];
                        Db::name("consigns")->insert($insert);
                    }else{
                        $up = [
                            "ub_id" => $uid,
                            "gid" => $gid,
                            "lid" => $good["lid"],
                            "price" => $bid
                        ];
                        Db::name("consigns")->where("cid", $wat["cid"])->update($up);
                    }
                    Code(200, "委托成功", (Object)[]);
                }
            }
        }else{
            Code(403, "服务器错误，请稍后再试");
        }

    }

    // 获取关注列表
    public function GetWatch(){
        $uid = $this->token["id"];
        $watched = Db::name("watched")->where("ub_id", $uid)->select();
        $data = [];
        if(!empty($watched)){
            foreach ($watched as $value){
                $good = Db::name("goods")->where("gid", $value["gid"])->find();
                $image = $this->SearchImg($good["lid"], $good["number"]);
                $data[] = [
                    "id" => $good["gid"],
                    "cover" => $image[0],
                    "final" => $good["final_price"] == 0 ? "" : $good["final_price"],
                    "lot" => $good["number"],
                    "name" => $good["name"],
                    "start" => number_format($good["price"], 0),
                    "start_unit" => "￥",
                    "state" => (int)$good["state"],
                ];
            }
        }
        Code(200, false, $data);
    }

    // 获取获拍的列表
    public function GetOrder(){
        $uid = $this->token["id"];
        $good = Db::name("goods")->where("state", 3)->where("ub_id", $uid)->select();
        $data = [];
        if(!empty($good)){
            foreach ($good as $value){
                $image = $this->SearchImg($value["lid"], $value["number"]);
                $data[] = [
                    "id" => $value["gid"],
                    "cover" => $image[0],
                    "final" => $value["final_price"] == 0 ? "" : $value["final_price"],
                    "lot" => $value["number"],
                    "name" => $value["name"],
                    "start" => number_format($value["price"], 0),
                    "start_unit" => "￥",
                    "state" => (int)$value["state"],
                ];
            }
        }
        Code(200, false, $data);
    }



    public function GetGoods(){
        $_GET["id"] = 1;
        if(isset($_GET["id"]) && isset($_GET["limit"]) && isset($_GET["page"])){
            $good = Db::name("goods")->where("lid", $_GET["id"])->page($_GET["page"], $_GET["limit"])->select();
            $count = Db::name("goods")->where("lid", $_GET["id"])->count();
            $goods = [];
            if(!empty($good)){
                foreach ($good as $value){
                    $goods[] = [
                        "id" => (int)$value["gid"],
                        "lot" => $value["number"],
                        "name" => $value["name"],
                        "start" => number_format($value["price"],0),
                        "start_unit" => "￥",
                        "state" => (int)$value["state"],
                        "cover" => is_file("./static/upload/image/{$value["number"]}-1.jpg") ? Config::get("app.ip")."static/upload/image/{$value["number"]}-1.jpg?t=".time() : Config::get("app.ip")."static/image/no_img.jpg?t=".time(),
                    ];
                }
            }
            $array = [
                "code" => 200,
                "data" => [
                    "list" => $goods,
                    "belong" => [
                        "id" => 1,
                        "name" => "古龍会专场",
                        "total" => $count
                    ]
                ]
            ];
        }else{
            $array = [
                "code" => 403,
                "msg" => "服务器错误，请稍后再试"
            ];
        }
        echo json_encode($array, 256);
    }

    public function GetGoodsDetail1(){
        if(isset($_GET["id"])){
            $good = Db::name("goods")->where("gid", $_GET["id"])->find();
            if(!empty($good)){
                $lot = Db::name("lot")->where("lid", $good["lid"])->where("is_over", 0)->find();
                if(!empty($lot)){
                    $array = [
                        "code" => 200,
                        "data" => [
                            "id" => (int)$good["gid"],
                            "lot" => $good["number"],
                            "name" => $good["name"],
                            "spec" => [
                                [
                                    "attr" => "尺寸",
                                    "val" => $good["year"],
                                ]
                            ],
                            "final" => $good["final_price"] == 0 ? "" : number_format($good["final_price"], 0),
                            "img" => [
                                is_file("./static/upload/image/{$good["number"]}-1.jpg") ? Config::get("app.ip")."static/upload/image/{$good["number"]}-1.jpg?t=".time() : Config::get("app.ip")."static/image/no_img.jpg?t=".time(),
                            ],
                            "start" => number_format($good["price"], 0),
                            "start_convert" => "￥ ".number_format((int)$good["price"]*(float)$lot["rate"], 0),
                            "start_unit" => "￥",
                            "state" => (int)$good["state"],
                            "watched" => false,
                            "belong" => [
                                "id" => (int)$good["lid"],
                                "name" => $lot["name"]
                            ],
                        ]
                    ];
                }else{
                    $array = [
                        "code" => 403,
                        "msg" => "商品不存在"
                    ];
                }
            }else{
                $array = [
                    "code" => 403,
                    "msg" => "商品不存在"
                ];
            }
        }else{
            $array = [
                "code" => 403,
                "msg" => "服务器错误，请稍后再试"
            ];
        }
        echo json_encode($array, 256);
    }


}
